package org.wanda.pv.service.conver.impl;

import org.icepdf.core.pobjects.Document;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wanda.pv.model.hy.HyDrawing;
import org.wanda.pv.service.conver.ToPngProcess;
import org.wanda.pv.service.file.FileUploadService;

import java.util.Map;

@Service
public class ToPngProcessImpl implements ToPngProcess{
    private final Logger log = LoggerFactory.getLogger(ToPngProcessImpl.class);

    @Autowired
    private Dao dao;
    @Autowired
    private FileUploadService fileUploadService;

    @Override
    public void process(Map<String, Object> map) {
        System.out.println("process is runing");

        String filePath = (String)map.get("FILE");
        Long id = (Long) map.get("ID");

        try{

            Map<String, Object> jpg_ = fileUploadService.pdf2jpg_(filePath);
            Map<String, Object> thumbnail_ = fileUploadService.pdf2Thumbnail_(filePath);

            Long fid = (Long) jpg_.get("fid");
            Long tfid =  (Long) thumbnail_.get("tfid");


            String fileName = dao.fetch(HyDrawing.class, id).getFileName();
            System.out.println("before::::::"+fileName);

            dao.update(HyDrawing.class, Chain.make("fid",fid).add("tfid",tfid).add("status",2), Cnd.where("id","=",id));
            fileName = dao.fetch(HyDrawing.class, id).getFileName();
            System.out.println("after::::::"+fileName);

        }catch (Exception e){
            log.error(e.getMessage(),e);
        }


    }
}
